package com.petAdoption.manager.controller;



import com.petAdoption.manager.entity.Employee;
import com.petAdoption.manager.service.IEmployeeService;
import com.petAdoption.manager.util.ResponseResult;
import com.petAdoption.manager.util.Result;
import com.petAdoption.manager.util.TokenUtil;
import com.petAdoption.manager.vo.EmployeeOneVO;
import com.petAdoption.manager.vo.EmployeeVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王倩茹
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/employee")

public class   EmployeeController {
    @Resource
    private IEmployeeService employeeService;
    @Resource
    private Result result;

    @PostMapping("/login")
    public Result login(@RequestBody EmployeeVO employeeVO) {
        Employee employee = new Employee();
        employee.setEmployeeUsername(employeeVO.getUsername());
        employee.setEmployeePassword(employeeVO.getPassword());
        Employee employee1 = employeeService.findEmployee(employee);
        if (employee1==null)return result.fail("账号密码不能为空");
        if (!employee1.getEmployeePassword().equals(employee.getEmployeePassword()))return result.fail("用户名或者密码错误");
        String token = TokenUtil.createToken(employee1.getEmployeeId(), employee1.getEmployeePassword());
        return result.success("登录成功",token);
    }

    @GetMapping("/getInfo")
    public Result getInfo(){
        Employee currenEmployee = TokenUtil.getCurrenEmployee();
        Employee employee = new Employee();
        employee.setEmployeeId(currenEmployee.getEmployeeId());
        employee.setEmployeePassword(currenEmployee.getEmployeePassword());
        employee.setEmployeeUsername(currenEmployee.getEmployeeUsername());
        Employee employee1 = employeeService.get(employee);
        EmployeeOneVO employeeOneVO = new EmployeeOneVO();
        employeeOneVO.setEmployeeId(employee1.getEmployeeId());
        employeeOneVO.setUsername(employee1.getEmployeeName());
        employeeOneVO.setRoles(employee1.getEmployeeName());
        if (employee1==null)return result.fail("验证失败");
        return result.success("验证成功",employeeOneVO);
    }

    @GetMapping("/logout")
    public Result getInfo(HttpServletResponse response){
        Cookie newCookie=new Cookie("token","");
        newCookie.setMaxAge(0);
        response.addCookie(newCookie);
        return result.success("成功退出");
    }




//    //注册
//    @GetMapping("/regexusername")
//    public ResponseResult regexUsername(@Param("username") String username) {
//        Employee employee = employeeService.regexUsername(username);
//        if (employee.getEmployeeuserName() == null) {
//            return ResponseResult.ok("恭喜，该账户还没有被注册");
//        }
//        return ResponseResult.fail("用户名重复");
//    }





    /**
     * 个人信息修改
     * @param employee
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Employee employee){
        employeeService.updateEmployee(employee);
        if (employee!=null){
            return ResponseResult.ok(employee);
        }else {
            return ResponseResult.error("修改失败");
        }

    }

}




